<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check="false"
      offset="50"
    >
      <ArticleItem v-for="(itm, i) in List" :key="i" :obj="itm" />
    </van-list>
  </div>
</template>

<script>
import { SearchList } from '@/api/home'
import ArticleItem from '../Layout/Home/components/ArticleItem'
export default {
  name: 'SearchResult',
  components: {
    ArticleItem
  },
  data () {
    return {
      List: [],
      loading: false,
      finished: false,
      page: 0
    }
  },
  methods: {
    onLoad () {
      this.getSearchList()
    },
    async getSearchList () {
      try {
        this.page++
        const res = await SearchList({
          q: this.$route.params.kw,
          page: this.page
        })
        this.loading = false
        this.List = [...this.List, ...res.data.results]
        console.log(this.List)
      } catch (error) {
        console.log('错误!')
      }
      if (this.List.length >= 30) {
        this.finished = true
      }
    }
  },
  async created () {
    this.getSearchList()
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
